package org.point.controller;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.CreditsRecord;
import org.point.domain.CreditsRule;
import org.point.domain.CreditsType;
import org.point.service.CreditsRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: CreditsRuleController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:16
 *
 */
@Controller
@RequestMapping("/creditsR/")
public class CreditsRuleController extends BaseController {

	@Autowired
	private CreditsRuleService service;

	/**
	 * 
	 * @Title: creditsRuleList @Description: TODO(积分规则查询) @param @param
	 *         memberId @param @param currentPage 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "crlist.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void creditsRuleList(@RequestParam(value = "memberId") String memberId,
			@RequestParam(value = "currentPage") Integer currentPage,
			@RequestParam(value = "creditsActionId") String creditsActionId,
			@RequestParam(value = "keyWord", required = false) String keyWord,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "starttime", required = false) String starttime,
			@RequestParam(value = "endtime", required = false) String endtime, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		DoPage<CreditsRule> page = new DoPage<CreditsRule>();
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (strOperateUtil.isNullForString(memberId)) {
				map.put("memberId", memberId);
				if (starttime != null && endtime != null) {
					map.put("starttime", starttime);
					map.put("endtime", endtime);

				}
				if (strOperateUtil.isNullForString(keyWord)) {
					map.put("keyWord", "%" + keyWord + "%");
				}
				if (strOperateUtil.isNullForString(status)) {
					map.put("status", status);
				}
				if (currentPage != null) {
					page.setCurrentPage(currentPage);
					map.put("firstRecord", page.getFirstRecord());
					map.put("pageSize", page.getPageSize());
					// if(strOperateUtil.isNullForString(creditsActionId)){
					map.put("creditsActionId", creditsActionId);
					page.setMap(map);
					DoPage<CreditsRule> resultPage = service.searchCRForPage(page);
					// 积分规则查询成功
					jsonObj.put("page", resultPage);
					jsonObj.put("resultMsg", 1);
				} else {
					// 分页当前页为null
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("积分规则查询错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: insertCR @Description: TODO(新增积分规则) @param @param record
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "crinsert.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertCR(@ModelAttribute("CreditsRule") CreditsRule record, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		record.setMemberId("member01");
		String crCode = getNextNo("CR");
		record.setDelFlag(new Byte("0"));
		try {
			result = service.insertCR(record, crCode);
			// 积分规则新增成功
			jsonObj.put("resultMsg", result);
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: editCRStatus @Description: TODO(修改积分规则状态) @param @param
	 *         creditsRuleId @param @param memberId @param @param status
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "creditstatus.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editCRStatus(@RequestParam(value = "creditsRuleId") String creditsRuleId,
			@RequestParam(value = "memberId") String memberId, @RequestParam(value = "status") Integer status,
			HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;

		try {
			if (strOperateUtil.isNullForString(creditsRuleId)) {
				map.put("creditsRuleId", creditsRuleId);
				// if(strOperateUtil.isNullForString(memberId)){
				map.put("memberId", memberId);
				if (status != null) {
					map.put("status", status);
					result = service.editCRStatus(map);
					// 修改状态成功
					jsonObj.put("resultMsg", result);
				} else {
					// 状态为null
					jsonObj.put("resultMsg", -3);
				}
				// } else {
				// 会员单位ID为null
				// jsonObj.put("resultMsg", -2);
				// }
			} else {
				// 积分规则ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("rsultMsg", result);
			throw new RuntimeException("积分规则修改状态错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: delCT @Description: TODO(批量删除积分规则分类) @param @param record
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "delCR.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delCR(@RequestParam(value = "ids") String ids, HttpServletResponse response) {
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length() - 1);
		try {
			map.put("newIDs", newIDs);
			result = service.deleteCRs(map);
			// result = service.deleteCT();
			if (result > 0) {
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: getCredits @Description: TODO(根据积分规则ID，查询详细信息) @param @param id
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "getCR.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getCredits(@RequestParam(value = "id") String id, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		try {
			if (strOperateUtil.isNullForString(id)) {
				map.put("id", Integer.parseInt(id));
				CreditsRule record = service.getCRByCRID(map);
				List<Hashtable<Object, Object>> credits_types = service.selectCreditsTypes();
				List<Hashtable<Object,Object>> credits_behavior = service.selectcreditsBehavior();
				jsonObj.put("credits_types", credits_types);
				jsonObj.put("credits_behavior", credits_behavior);
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询积分规则详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	@RequestMapping(value = "openWindows.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void openWindows( HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		try {
			List<Hashtable<Object,Object>> credits_types = service.selectCreditsTypes();
			List<Hashtable<Object,Object>> credits_behavior = service.selectcreditsBehavior();
			if(credits_behavior!=null){
				jsonObj.put("credits_behavior", credits_behavior);
			}
			if(credits_types!=null){
				jsonObj.put("credits_types", credits_types);
				// 查询兑换规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			}else{
				// 兑换规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询兑换规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	
}
